package com.estimote.oaplugin;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.zywx.wbpalmstar.engine.EBrowserView;
import org.zywx.wbpalmstar.engine.universalex.EUExBase;
import org.zywx.wbpalmstar.engine.universalex.EUExCallback;

import com.estimote.sdk.Beacon;
import com.estimote.sdk.BeaconManager;
import com.estimote.sdk.Region;
import com.estimote.sdk.BeaconManager.MonitoringListener;

import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.Intent;
import android.os.RemoteException;
import android.util.Log;
import android.webkit.JavascriptInterface;
import android.widget.Toast;

public class UexHandler extends EUExBase{
	
	static final String func_enter_callback = "uexOA.func_enter_callback";
	static final String func_leave_callback = "uexOA.func_leave_callback";
	static final String func_on_callback = "javascript:uexOA.onCallBack";
	
	private BeaconManager beaconManager;
	private Region region;
	

	public UexHandler(Context context, EBrowserView view) {
		super(context, view);
		
		region = new Region("regionId", null, null, null);
		beaconManager = new BeaconManager(context);
		beaconManager.setBackgroundScanPeriod(TimeUnit.SECONDS.toMillis(1), 0);
		beaconManager.setMonitoringListener(new MonitoringListener(){
			@Override
			public void onEnteredRegion(Region arg0, List<Beacon> arg1) {
				System.out.println("进入信标范围");
				Toast.makeText(mContext, "进入信标范围.", 1000).show();
				enterCallBack("你已进入信标范围");
			}
			@Override
			public void onExitedRegion(Region arg0) {
				System.out.println("离开信标范围");
				Toast.makeText(mContext, "离开信标范围", 1000).show();
				leaveCallBack("你已离开信标范围");
			}
		});
	}

	@JavascriptInterface
	public void openService(String[] parm) {
		beaconManager.connect(new BeaconManager.ServiceReadyCallback() {
			@Override
			public void onServiceReady() {
				Toast.makeText(mContext, "111111.....", 1000).show();
				
				BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
				if(mBluetoothAdapter.isEnabled()==false){
					mBluetoothAdapter.enable();
				}
				
				try {
					beaconManager.startMonitoring(region);
				} catch (RemoteException e) {
					//Log.d("----go----", "Error while starting monitoring");
					Toast.makeText(mContext, "Error while starting monitoring", 1000).show();
				}
			}
		});
	}
	
	@JavascriptInterface
	public void debug(String str) {
		Toast.makeText(mContext, "debug.....", 1000).show();
	}


	// input dialog result
	protected void enterCallBack(String result) {
		Log.i("----enterCallBack----", "enterCallBack");
		jsCallback(func_enter_callback, 0, EUExCallback.F_C_TEXT, result);
	}
	
	protected void leaveCallBack(String result){
		jsCallback(func_leave_callback, 0, EUExCallback.F_C_TEXT, result);
	}

	// clean something
	@Override
	protected boolean clean() {
//		if (null != m_v) {
//			try {
//				m_v.cancel();
//			} catch (SecurityException e) {
//				
//			}
//		}
//		m_v = null;
		return true;
	}

}